package com.hzlh.controller.pc;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzlh.model.*;
import com.hzlh.service.ICompanyBaseService;
import com.hzlh.service.ICompanyProductService;
import com.hzlh.service.IFinancingHistoryService;
import com.hzlh.utils.AddressUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/companyBase")
public class CompanyBaseController {
    @Autowired
    private ICompanyBaseService companyBaseService;
    @Autowired
    private ICompanyProductService companyProductService;
    @Autowired
    private IFinancingHistoryService financingHistoryService;

    /**
     * 查询企业名称列表
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @GetMapping("/getList")
    public PageInfo getList(@RequestParam(value = "attributionArea", defaultValue = "", required = false) String attributionArea,
                            @RequestParam(value = "attributionZone", defaultValue = "", required = false) String attributionZone,
                            @RequestParam(value = "companyName", defaultValue = "", required = false) String companyName,
                            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        HashMap<String, Object> param = new HashMap<>();
        param.put("attributionArea",attributionArea);
        param.put("attributionZone",attributionZone);
        param.put("companyName",companyName);
        List<TCompanyBaseVo> tCompanyBaseVos = companyBaseService.selectList(param);
        return new PageInfo<>(tCompanyBaseVos);
    }

    /**
     * 查询企业详情
     *
     * @param companyName
     * @return
     */
    @GetMapping("/getInfo")
    public Object getInfo(@RequestParam(value = "companyName", defaultValue = "", required = true) String companyName) {
        TCompanyBase tCompanyBase = companyBaseService.selectInfo(companyName);
        TCompanyBaseDto tCompanyBaseDto = new TCompanyBaseDto();
        if (tCompanyBase != null){
            BeanUtils.copyProperties(tCompanyBase,tCompanyBaseDto);
            List<String> companyLabel = new ArrayList<>();
            companyLabel.add(tCompanyBase.getTaxOverOne());
            companyLabel.add(tCompanyBase.getTaxOverTwo());
            companyLabel.add(tCompanyBase.getServeKeyCci());
            companyLabel.add(tCompanyBase.getEnterpriseClass());
            companyLabel.add(tCompanyBase.getPredictMarkeringEnterprises());
            companyLabel.add(tCompanyBase.getEffectiveNationalHightechEnterprises());
            companyLabel.add(tCompanyBase.getApplyNationalHightechEnterprises());
            companyLabel.add(tCompanyBase.getFiveAffiliatedEnterprises());
            companyLabel.add(tCompanyBase.getMaturity());
            companyLabel.removeAll(Collections.singleton(null));
            tCompanyBaseDto.setCompanyLabels(companyLabel);
        }
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("companyName",companyName);
        List<TCompanyProduct> tCompanyProducts = companyProductService.selectList(hashMap);
        if (!tCompanyProducts.isEmpty()){
            tCompanyBaseDto.setCompanyProducts(tCompanyProducts);
        }
        List<TFinancingHistory> tFinancingHistories = financingHistoryService.selectList(hashMap);
        if (!tFinancingHistories.isEmpty()){
            tCompanyBaseDto.setTFinancingHistories(tFinancingHistories);
        }
        return tCompanyBaseDto;
    }

    /**
     * 企业区域统计
     * @return
     */
    @GetMapping("/areaCount")
    public Object areaCount(){
        List<Map<String, Object>> areaMap = companyBaseService.selectAreaCount();
        List<Map<String, Object>> zoneMap = companyBaseService.selectZoneCount();
        List<Map<String, Object>> companyMap = companyBaseService.selectCompanyCount();
        ArrayList<List<Map<String, Object>>> lists = new ArrayList<>();
        lists.add(areaMap);
        lists.add(zoneMap);
        lists.add(companyMap);
        return lists;
    }

    /**
     * 更新企业区域街道地址
     * @return
     */
    @GetMapping("/updateArea")
    public Object updateArea() {
        List<TCompanyBaseVo> tCompanyBaseVos = companyBaseService.selectList(null);
        tCompanyBaseVos.stream().forEach(v -> {
            Map<String, String> map = AddressUtil.getAddressDetail(v.getLocLon().doubleValue(), v.getLocLat().doubleValue());
            v.setAttributionArea(map.get("district"));
            v.setAttributionZone(map.get("town"));
            int i = companyBaseService.updateArea(v);
            String s = i == 1 ? "success" : "fail";
            System.out.println(map.get("town") + " : 导入" + s);
        });
        return "success";
    }
}
